
## Today's Packages and Data 🤗


```{r}
# run for packages that you have not installed yet
# install.packages("tidyverse")
install.packages("effectsize")
```


```{r}
library(effectsize)
library(car)
library(tidyverse)
# I am setting a theme for all the plots
theme_set(theme_classic(base_size = 16, 
                        base_family = 'serif'))
```



```{r}
dat <- rio::import("https://fabio-setti.netlify.app/data/Cuisine_rating.csv")
```

## So...?


```{r}
ggplot() +
  geom_function(fun = dt, args = list(df = 199), color = "#7a0b80") +
  labs(x = "All Possible t statistics if H0 is true") +
  xlim(-3, 3) +
  geom_vline(xintercept = 2, lty = 2, col = "red") +
  geom_vline(xintercept = -2, lty = 2, col = "red") +
  annotate("text", x = 0, y = .2, label = paste("Values beyond the red dotted lines \n are unlikely to happen if H0 is true"), size = 5) +
  scale_y_continuous(expand = c(0,0)) +
  theme(axis.title.y=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank(),
        axis.line.y =  element_blank())
```


## One-Sample $t$-test by Hand


```{r}
mean <- mean(dat$Food_Rating)
mean

S <- sd(dat$Food_Rating)
S

SE <- S/sqrt(nrow(dat))
SE

t <- (mean - 3)/SE
t

df <- nrow(dat) - 1
df

(1 - pt(t, df = df))*2
```


## A graphical representation


```{r}
ggplot() +
  geom_function(fun = dt, args = list(df = df), color = "#7a0b80") +
  labs(x = "All Possible t statistics if H0 is true") +
  xlim(-3, 3) +
  geom_vline(xintercept = t, lty = 2, col = "red") +
  annotate("text", x = t + .5, y = .1, label = paste("Our t-value, \n", round(t, 2)), size = 5) +
  scale_y_continuous(expand = c(0,0)) +
  theme(axis.title.y=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank(),
        axis.line.y =  element_blank())
```


## One-Sample $t$-test in R


```{r}

t.test(dat$Food_Rating, 
       mu = 3)
```

## Cohen's $d$ for One-Sample $t$-tests


```{r}
# small effect size
(mean(dat$Food_Rating) - 3)/sd(dat$Food_Rating)
```

## Cohen's $d$ With `effectsize`


```{r}
cohens_d(dat$Food_Rating,
         mu = 3)
```


## Paired-Samples $t$-test in R 


```{r}
t.test(dat$Service_Rating, dat$Food_Rating,
      paired = TRUE)
```


```{r}
t.test(dat$Service_Rating - dat$Food_Rating)
```


## Independent-Samples $t$-test in R (Welch)


```{r}
t.test(Overall_Rating ~ Gender, 
       data = dat)
```


## Equal Variances Assumed


```{r}
t.test(Overall_Rating ~ Gender, 
       data = dat,
       var.equal = TRUE)
```


## Independent-Samples $t$-test: Cohen's $d$


```{r}
cohens_d(Overall_Rating ~ Gender, 
         data = dat,
         pooled_sd = FALSE)

cohens_d(Overall_Rating ~ Gender, 
         data = dat)
```


## Normality of Groups: Descriptives


```{r}
dat %>% 
  group_by(Gender) %>%
      summarise(mean = mean(Overall_Rating), 
                sd = sd(Overall_Rating),
                skew = e1071::skewness(Overall_Rating), 
                kurt = e1071::kurtosis(Overall_Rating))
```


## Normality of Groups: QQplot


```{r}
ggplot(dat,  
       aes(sample = Overall_Rating)) +
       stat_qq() + 
       stat_qq_line() +
       # facet_wrap() will create plots for each of the levels of Gender!
       facet_wrap(~ Gender)
```


## Normality of Groups: Density Plots

```{r}
ggplot(dat,  
       aes(x = Overall_Rating)) +
       geom_density()+
       facet_wrap(~ Gender)
```


## Outliers: Trimmed Mean


```{r}
dat %>% 
  group_by(Gender) %>%
    summarize(mean = mean(Overall_Rating),
              #takes out the top and bottom 2.5%
              tmean = mean(Overall_Rating, trim = .05))
```


## Outliers: Standardized Units


```{r}
dat %>% 
  # create standardized version of Overall_Rating
  mutate(Overall_Rating_std = (Overall_Rating - mean(Overall_Rating))/sd(Overall_Rating)) %>% 
    group_by(Gender) %>% 
    # count how many have absolute value above 3
     summarise(N_extreme = sum(abs(Overall_Rating_std) > 3))
```

## Wilcoxon Signed-Rank Test

```{r}
wilcox.test(Overall_Rating ~ Gender, 
            data = dat)
```